<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	require_once ('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];

    $done = $_REQUEST[done];
    $id= $_REQUEST[id];
    if ($done==0) {
    $rResult = mysql_query("SELECT  par.`id`,
                                    par.`name`,
                                    round(SUM(b_letter_details.quantity)),
                                    SUM(b_letter_details.self_cost)
                        FROM 	warehouse_producing
                        JOIN  production ON warehouse_producing.production_id = production.id
                        JOIN  production_category ON production.category=production_category.id
                        JOIN  production_category as par ON par.id=production_category.parent_id
                        JOIN  b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_producing.warehouse_detail_id AND warehouse_producing.cartridge_part_id=b_letter_details.part_type_id
                        WHERE (DATE(warehouse_producing.date) BETWEEN '$start' and '$end')
                        GROUP BY   par.name
                        UNION ALL
                        SELECT      par.`id`,
                                    par.`name`,
                                    count(*),
                                    sum((SELECT  ROUND(AVG(reserve.price),2) FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id))

                        FROM	warehouse_producing
                        JOIN  production ON warehouse_producing.production_id = production.id
                        JOIN  production_category ON production.category=production_category.id
                        JOIN  production_category as par ON par.id=production_category.parent_id
                        WHERE NOT ISNULL(warehouse_producing.printer_part_id) AND (DATE(warehouse_producing.date) BETWEEN '$start' and '$end')
                        GROUP BY   par.name");
    }else if($done==1){
      $rResult = mysql_query("SELECT        production_category.`id`,
                                            production_category.`name`,
                    						round(SUM(b_letter_details.quantity)),
                    						ROUND(MIN(b_letter_details.self_cost),2),
                    			            ROUND(AVG(b_letter_details.self_cost),2),
                    						ROUND(MAX(b_letter_details.self_cost),2),
                                            ROUND(SUM(b_letter_details.self_cost),2)
                    FROM 	warehouse_producing
                    JOIN  production ON warehouse_producing.production_id = production.id
                    JOIN  production_category ON production.category=production_category.id
                    JOIN  b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_producing.warehouse_detail_id  AND warehouse_producing.cartridge_part_id=b_letter_details.part_type_id
                    WHERE (DATE(warehouse_producing.date) BETWEEN '$start' and '$end') AND parent_id=$id
                    GROUP BY   production_category.name
                    UNION ALL
                    SELECT  production_category.`id`,
                    		production_category.`name`,
                    		count(*),
                    		ROUND(MIN((SELECT  MIN(reserve.price) FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2),
                    		ROUND(AVG((SELECT  AVG(reserve.price) FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2),
                    		ROUND(MAX((SELECT  MAX(reserve.price) FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2),
                            ROUND(SUM((SELECT  AVG(reserve.price) FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2)

                    FROM	warehouse_producing
                    JOIN  production ON warehouse_producing.production_id = production.id
                    JOIN  production_category ON production.category=production_category.id
                    WHERE NOT ISNULL(warehouse_producing.printer_part_id)
                          AND (DATE(warehouse_producing.date) BETWEEN '$start' and '$end') AND parent_id=$id
                    GROUP BY   production_category.name");
    }
    else{
      $rResult = mysql_query("SELECT    production.`id`,
                    					production.`name`,
                    				    round(SUM(b_letter_details.quantity)),
                    				    round((b_letter_details.self_cost / b_letter_details.quantity),2),
                                        ROUND(SUM(b_letter_details.self_cost),2)
                    FROM 	warehouse_producing
                    JOIN  production ON warehouse_producing.production_id = production.id
                    JOIN  b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_producing.warehouse_detail_id  AND warehouse_producing.cartridge_part_id=b_letter_details.part_type_id
                    WHERE (DATE(warehouse_producing.date) BETWEEN '$start' and '$end')
                    AND   production.category = $id
                    GROUP BY   production.id

                    UNION ALL

                    SELECT              production.`id`,
                    					production.`name`,
                    					count(*),
                                        ROUND(AVG((SELECT  AVG(reserve.price)		FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2),
                                        ROUND(SUM((SELECT  AVG(reserve.price)		FROM reserve  WHERE reserve.production_id = warehouse_producing.production_id)),2)
                    FROM	warehouse_producing
                    JOIN  production ON warehouse_producing.production_id = production.id
                    WHERE NOT ISNULL(warehouse_producing.printer_part_id)
                    	  AND (DATE(warehouse_producing.date) BETWEEN '$start' and '$end')
                    			AND production.category = $id
                    GROUP BY   production.id");

    }

	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];


			$output = array(
				"aaData"	=> array()
			);
			$count=mysql_num_fields ($rResult);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
			    $aRow[$count]=(round($aRow[2]/$_REQUEST[qv]*100))."%";
				$output['aaData'][] = $aRow;
			}

			echo json_encode( $output );

	        break;
	    case 'chart':


	    	$output = array(
	    			"aaData"	=> array()
	    	);
	    	$data = array();
	    	while ( $aRow = mysql_fetch_array( $rResult ) )
	    	{

	    			$data[xAxis][categories][] = $aRow[1];
	    			$data[series][0][data][] = [$aRow[1],(float) $aRow[2]];

	    	}
	    	$data[plotOptions][pie][dataLabels][format]='<b>{point.name}</b>: {point.percentage:.1f} %';
	    	$data[series][0][type]='pie';
	    	echo json_encode( $data );
	    	break;

	    default:
	       	echo "null";
	}
?>